package javao.cn.work.orc.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javao.cn.work.orc.common.exception.OrcException;
import javao.cn.work.orc.common.responsTool.Res;
import javao.cn.work.orc.pojo.survey.OrcSurvey;
import javao.cn.work.orc.service.AF150SurveyService;
import javao.cn.work.orc.utils.pdf.PdfUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description AF150 ORC远程调用接口
 * @Author Admin小闫
 * @Date 2024/12/6
 */
@Api(tags = "AF150 PDF测量报告ORC识别")
@ApiSupport(author = "Admin小闫", order = 1)
@RestController
@RequestMapping("/orc/AF150")
@Slf4j
public class AF150SurveyController {

    @Autowired
    private AF150SurveyService af150SurveyService;

    @Autowired
    private PdfUtils pdfUtils;

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/single")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "ORC识别单个文件", notes = "单个文件上传大小不得超过3MB，多个文件上传总大小不可超过15MB")
    public Res singleFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Res.error("文件为空");
        }
        try {
            Map<Integer,String> map=new HashMap<>();
            // 获取文件类型
            String originalFilename = file.getOriginalFilename();
            String fileExtension = originalFilename != null ? originalFilename.substring(originalFilename.lastIndexOf(".")) : "";
            if (!fileExtension.equals(".pdf")){
                throw new OrcException("文件类型错误！您上传的【"+originalFilename+"】不是PDF类型文件！,请上传PDF类型文件！");
            }
            Map<Integer, BufferedImage> images = pdfUtils.pdf2png(file.getInputStream());
            log.debug("转换图片成功！");
            OrcSurvey orcSurvey=af150SurveyService.orcSurvey(images);
            log.debug("识别成功："+orcSurvey.toString());
            return Res.ok(orcSurvey);
        } catch (IOException e) {
            log.error("识别失败: ", e);
            return Res.error("文件上传失败");
        }
    }







    /**
     * 通用上传请求（多个）
     */
    @PostMapping("/multiple")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "ORC识别多个文件", notes = "单个文件上传大小不得超过3MB，多个文件上传总大小不可超过15MB")
    public Res multipleFiles(@RequestParam("files") List<MultipartFile> files){
        /* 文件----校验 */
        if (files == null || files.isEmpty()) {
            return Res.error("没有选择文件");
        }
        for (MultipartFile file : files) {
            if (file.isEmpty()) {
                return Res.error("存在空文件");
            }
            // 获取文件类型
            String originalFilename = file.getOriginalFilename();
            String fileExtension = originalFilename != null ? originalFilename.substring(originalFilename.lastIndexOf(".")) : "";
            if (!fileExtension.equals(".pdf")){
                throw new OrcException("文件类型错误！您上传的【"+originalFilename+"】不是PDF类型文件！,请上传PDF类型文件！");
            }
        }

        /* 识别文件内容 */
        try {
            List<OrcSurvey> list=new ArrayList<>();
            for (MultipartFile file : files) {
                //开始PDF转换为png图片
                Map<Integer, BufferedImage> images = pdfUtils.pdf2png(file.getInputStream());
                log.debug("转换图片成功！");
                OrcSurvey orcSurvey=af150SurveyService.orcSurvey(images);
                log.debug("识别成功："+orcSurvey.toString());
                list.add(orcSurvey);
            }
            return Res.ok(list);
        } catch (IOException e) {
            log.error("文件识别失败: ", e);
            return Res.error("文件识别失败"+e.getMessage());
        }
    }
    
    
    
}
